package com.gqj.meter;


import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.StorageUtil;
import com.gqj.meter.domain.*;
import com.gqj.meter.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component("SaticScheduleTask")
public class SaticScheduleTask {

    @Autowired
    private WarningConfigMapper warningConfigMapper;
    @Autowired
    private MdMeterMapper mdMeterMapper;
    @Autowired
    private MdWarningInfoMapper mdWarningInfoMapper;
    @Autowired
    private MdLcEquipMapper mdLcEquipMapper;
    @Autowired
    private MdItMapper mdItMapper;
    @Autowired
    private MdConcentratorMapper mdConcentratorMapper;
    @Autowired
    private MdComModuleMapper mdComModuleMapper;
    @Autowired
    private MdOtherMaterialMapper mdOtherMaterialMapper;
    @Autowired
    private SysDataBackupRecordMapper sysDataBackupRecordMapper;

    @Value("${filePath}")
    private String filePath;
    @Value("${dbName}")
    private String dbName;
    @Value("${dbUsername}")
    private String dbUsername;
    @Value("${dbPassword}")
    private String dbPassword;
    @Value("${dbIp}")
    private String dbIp;


    @Transactional
    public void warningTasks() {
        //先查询报警配置表信息
        WarningConfig warningConfigQuery = new WarningConfig();
        List<WarningConfig> configList = warningConfigMapper.selectWarningConfigList(warningConfigQuery);

        //循环配置信息
        for (WarningConfig warningConfig : configList) {
            //检查是否开启报警
            String isOpen = warningConfig.getIsOpen();
            if ("1".equals(isOpen)) {
                //检查报警类型
                String configType = warningConfig.getConfigType();
                //获取本次配置上限下限
                int lowerLimit = warningConfig.getLowerLimit();
                int upperLimit = warningConfig.getUpperLimit();

                String[] serviceStatuss = {"01", "02", "03"};

                switch (configType) {
                    case "01":
                        MdMeter mdMeterQuery = new MdMeter();
                        mdMeterQuery.setServiceStatuss(serviceStatuss);
                        this.insertWarning(mdMeterMapper.selectMdMeterCount(mdMeterQuery), lowerLimit, upperLimit, "02", "电能表数量", DateUtils.getNowDate());
                        break;
                    case "02":
                        MdIt MdItQuery = new MdIt();
                        MdItQuery.setServiceStatuss(serviceStatuss);
                        this.insertWarning(mdItMapper.selectMdItCount(MdItQuery), lowerLimit, upperLimit, "02", "互感器数量", DateUtils.getNowDate());
                        break;
                    case "03":
                        MdLcEquip mdLcEquipQuery = new MdLcEquip();
                        mdLcEquipQuery.setServiceStatuss(serviceStatuss);
                        this.insertWarning(mdLcEquipMapper.selectMdLcEquipCount(mdLcEquipQuery), lowerLimit, upperLimit, "02", "终端数量", DateUtils.getNowDate());
                        break;
                    case "04":
                        MdConcentrator mdConcentratorQuery = new MdConcentrator();
                        mdConcentratorQuery.setServiceStatuss(serviceStatuss);
                        this.insertWarning(mdConcentratorMapper.selectMdConcentratorCount(mdConcentratorQuery), lowerLimit, upperLimit, "02", "集中器数量", DateUtils.getNowDate());
                        break;
                    case "05":
                        //退回状态
                        MdComModule mdComModuleQuery = new MdComModule();
                        mdComModuleQuery.setServiceStatuss(serviceStatuss);
                        this.insertWarning(mdComModuleMapper.selectMdComModuleCount(mdComModuleQuery), lowerLimit, upperLimit, "02", "模块数量", DateUtils.getNowDate());
                        break;
                    case "10":
                        //退回状态
                        MdOtherMaterial mdOtherMaterialQuery = new MdOtherMaterial();
                        mdOtherMaterialQuery.setServiceStatuss(serviceStatuss);
                        this.insertWarning(mdOtherMaterialMapper.selectMdOtherMaterialCount(mdOtherMaterialQuery), lowerLimit, upperLimit, "02", "其他物资数量", DateUtils.getNowDate());
                        break;
                }
            }
        }
    }

    private void insertWarning(int count, int lowerLimit, int upperLimit, String warningType, String warningDescription, Date warningDate) {
        //判断总数和上下限的关系
        if (count < lowerLimit) {
            //存入报警信息
            MdWarningInfo mdWarningInfo = new MdWarningInfo();
            mdWarningInfo.setWarningType(warningType);
            mdWarningInfo.setWarningDescription(warningDescription + "低于下限");
            mdWarningInfo.setWarningDate(warningDate);
            mdWarningInfoMapper.insertMdWarningInfo(mdWarningInfo);
        } else if (count > upperLimit) {
            //存入报警信息
            MdWarningInfo mdWarningInfo = new MdWarningInfo();
            mdWarningInfo.setWarningType(warningType);
            mdWarningInfo.setWarningDescription(warningDescription + "高于上限");
            mdWarningInfo.setWarningDate(warningDate);
            mdWarningInfoMapper.insertMdWarningInfo(mdWarningInfo);
        }
    }

    /**
     * 主动数据备份记录
     */
    public void generateRecord() {
        //备份
        boolean isFlag = StorageUtil.generateRecord(filePath, dbName, dbUsername, dbPassword, dbIp);
        //记录数据备份
        if (isFlag) {
            SysDataBackupRecord sysDataBackupRecord = new SysDataBackupRecord();
            sysDataBackupRecord.setBackupTime(DateUtils.getNowDate());
            sysDataBackupRecord.setBackupType(0);
            sysDataBackupRecord.setBackupUrl(filePath);
            sysDataBackupRecord.setBackupUserId((long) 1);
            sysDataBackupRecord.setDelflag(0);
            sysDataBackupRecordMapper.insertSysDataBackupRecord(sysDataBackupRecord);
        }
    }
}
